package com.program.houai.advice;

import com.program.houai.exception.BaseException;
import com.program.houai.exception.BusinessException;
import com.program.houai.exception.ErrorType;
import com.program.houai.exception.ParamException;
import com.program.houai.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;

@RestControllerAdvice
@Slf4j
public class ExceptionAdvice {
    /**
     * 基础异常
     */
    @ExceptionHandler(BaseException.class)
    public R baseException(BaseException e) {
        log.warn(e.getMessage(), e);
        return R.failed(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public R Exception(Exception e) {
        log.warn(e.getMessage(), e);
        return R.failed(e.getMessage());
    }

    /**
     * 业务异常
     */
    @ExceptionHandler(BusinessException.class)
    public R businessException(BusinessException e) {
        log.warn(e.getMessage(), e);
        return R.failed( e.getMessage());
    }

    @ExceptionHandler(RuntimeException.class)
    public R RunException(RuntimeException e) {
        log.warn(e.getMessage(), e);
        return R.failed(e.getMessage());
    }

    @ExceptionHandler(ParamException.class)
    public R ParamException(ParamException e) {
        log.warn(e.getMessage(), e);
        return R.restResult(null,Integer.parseInt(ErrorType.PARAM_USER_AUTH.getCode()),e.getMessage());
    }

    @ExceptionHandler(NullPointerException.class)
    public R handleNullPointerException(NullPointerException e) {
        log.warn(e.getMessage(), e);
        return R.failed("NullPointerException occurred:"+e.getMessage());
    }
    @ExceptionHandler(NumberFormatException.class)
    public R handleNumberFormatException(NumberFormatException e) {
        log.warn(e.getMessage(), e);
        return R.failed("NumberFormatException occurred: "+e.getMessage());
    }
}
